package net.lcheng.shop.Controller;

import com.alibaba.fastjson.JSONObject;
import net.lcheng.model.BaseAreas;
import net.lcheng.model.WxShop;
import net.lcheng.service.BaseAreasService;
import net.lcheng.service.WxShopService;
import net.lcheng.shop.vo.CurrentUserModel;
import net.lcheng.shop.vo.ProductUnityFormatModel;
import net.lcheng.vo.WxShopModel;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;

/**
 * Created by zwr on 2016/7/16 0016.
 */
@Controller
public class WxShopController {
    @Autowired
    private WxShopService wxShopService;
    @Autowired
    private BaseAreasService baseAreasService;
    /***
     * 店铺申请get
     */
    @RequestMapping(value = "wx/shop", method = RequestMethod.GET)
    public String wxShopGet(Model model) {
        Subject currentUser = SecurityUtils.getSubject();
        //判断当前用户有没有登录
        if (currentUser.isAuthenticated()) {
            //获取当前用户
            Session session = currentUser.getSession();
           CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
            WxShop shop = wxShopService.getShopInforByUserId(currentUserModel.getUserId());
            if (shop!=null){
                model.addAttribute("shop",shop);
                model.addAttribute("userIdentity",1);
            }
            model.addAttribute("WxShopModel", new WxShopModel());
        } else {
            return "redirect:/login";
        }
        return "wx_shop";
    }
    /***
     * 店铺申请post
     */
    @RequestMapping(value = "wx/shop", method = RequestMethod.POST)
    public String wxShopPost(Model model, @Valid @ModelAttribute("WxShopModel") WxShopModel wxShopModel, BindingResult result) {
        if (result.hasErrors()) {
            return "wx_shop";
        }
        /**获取省名称*/
        BaseAreas provinceCode = baseAreasService.getAreaCodeByAreaName(wxShopModel.getAddrProvinceCode());
        /**获取市名称*/
        BaseAreas cityCode = baseAreasService.getAreaCodeByAreaName(wxShopModel.getAddrCityCode());
        /**获取区名称*/
        BaseAreas districtCode = baseAreasService.getAreaCodeByAreaName(wxShopModel.getAddrDistrictCode());
        Subject currentUser = SecurityUtils.getSubject();
        //获取当前用户
        Session session = currentUser.getSession();
       CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
        WxShop shop = wxShopService.getShopInforByUserId(currentUserModel.getUserId());
        //判断如果申请了店铺就跳转提示用户不能重复申请
        if (shop!=null){
            return "redirect:/wx/shop";
        }else
        //判断用户以前没有申请过店铺的时候才能申请
        if (shop==null){
        String shopName = wxShopModel.getShopName();//店铺名
        String shopUserUserId = currentUserModel.getUserId();
        String name = wxShopModel.getName();//负责人
        String phone = wxShopModel.getPhone();//负责人手机号
        String AddrProvinceCode = wxShopModel.getAddrProvinceCode();//省编码
        String AddrProvinceName = provinceCode.getAreaName();
        String AddrCityCode = wxShopModel.getAddrCityCode();//市编码
        String AddrCityName = cityCode.getAreaName();
        String AddrDistrictCode = wxShopModel.getAddrDistrictCode();//区编码
        String AddrDistrictName = districtCode.getAreaName();
        String shopSummary = wxShopModel.getShopSummary();//店铺介绍
        String shopMainProduct = wxShopModel.getShopMainProduct();//店铺主营
        int returnResult = wxShopService.insert(shopName, shopUserUserId, name, phone, AddrProvinceCode, AddrProvinceName, AddrCityCode, AddrCityName, AddrDistrictCode, AddrDistrictName, shopSummary, shopMainProduct);
        if (returnResult>0){
            WxShop wxShop=wxShopService.getShopInforByUserId(shopUserUserId);
            model.addAttribute("wxShop",wxShop);
            model.addAttribute("returnResult",1);
        }
        }
        return "wx_shop";
    }
    /***
     * 店铺模拟支付成功
     */
    @RequestMapping(value = "wx/shop_success", method = RequestMethod.GET)
    public String wxShopSuccessGet(Model model,@RequestParam Integer shopId) {
        if (shopId==null){
            return "redirect:/my";
        }
      WxShop wxShop=new WxShop();
        wxShop.setShopId(shopId);
        wxShop.setApplyStatus(1);
        wxShop.setPayStatus(1);
        wxShop.setShopStatus(2);
       int returnResult= wxShopService.update(wxShop);
        if (returnResult>0){
            WxShop shop=wxShopService.getWxShopByShopId(shopId);
            model.addAttribute("shop",shop);
        }
        return "shop_success";
    }
    /***
     * 店铺设置get
     */
    @RequestMapping(value = "wx/shop_setup", method = RequestMethod.GET)
    public String wxShopSetup(Model model) {
        Subject currentUser = SecurityUtils.getSubject();
        //判断当前用户有没有登录
        if (currentUser.isAuthenticated()) {
            //获取当前用户
            Session session = currentUser.getSession();
           CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
            WxShop shop = wxShopService.getShopInforByUserIdAndShopStatus(currentUserModel.getUserId(),2);
            /**如果用户没有店铺就跳转首页*/
            if (shop==null){
                return "redirect:/home";
            }
            if (shop!=null){
                model.addAttribute("shop",shop);
            }
            model.addAttribute("WxShopModel", new WxShopModel());
        } else {
            //判断有没有登录没有登录就跳转登录页面
            return "redirect:/login";
        }
        return "shop_setup";
    }
    /***
     * 修改店铺logo
     */
    @RequestMapping(value = "modify/shop_logo", method = RequestMethod.GET)
    public String modifyShopLogoGet(Model model) {
        Subject currentUser = SecurityUtils.getSubject();
        //判断当前用户有没有登录
        if (currentUser.isAuthenticated()) {
            //获取当前用户
            Session session = currentUser.getSession();
           CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
            WxShop shop = wxShopService.getShopInforByUserIdAndShopStatus(currentUserModel.getUserId(),2);
            /**如果用户没有店铺就跳转首页*/
            if (shop==null){
                return "redirect:/home";
            }
            //判断用户有没有店铺
            if (shop!=null){
                model.addAttribute("shop",shop);
            }
            model.addAttribute("WxShopModel", new WxShopModel());
        } else {
            //判断有没有登录没有登录就跳转登录页面
            return "redirect:/login";
        }
        return "shop_logo";
    }
    /***
     * 修改店铺logo
     */
    @RequestMapping(value = "modify/shop_logo", method = RequestMethod.POST)
    public String modifyShopLogoPost(Model model,  @Valid @ModelAttribute("WxShopModel") WxShopModel wxShopModel, BindingResult result) {
        Subject currentUser = SecurityUtils.getSubject();
        //判断当前用户有没有登录
        if (currentUser.isAuthenticated()) {
            //获取当前用户
            Session session = currentUser.getSession();
           CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
            WxShop shop = wxShopService.getShopInforByUserIdAndShopStatus(currentUserModel.getUserId(),2);
            //这里判断用户自己有没有店铺
            if (shop != null) {
                //这判断他有没有设置图片如果没有设置图片就返回
                if (wxShopModel.getShopLogo()!=null) {
                    WxShop wxShop = new WxShop();
                    wxShop.setShopId(shop.getShopId());
                    wxShop.setShopLogo(wxShopModel.getShopLogo());
                    int returnResult = wxShopService.update(wxShop);
                    if (returnResult > 0) {
                        return "redirect:/wx/shop_setup";
                    }
                } else {
                    model.addAttribute("error", 1);
                    return "shop_logo";
                }
            }
        }else{
            return "redirect:/login";
        }
        return "shop_logo";
    }
    /***
     * 修改店铺名Get
     */
    @RequestMapping(value = "modify/shop_name", method = RequestMethod.GET)
    public String modifyShopNameGet(Model model) {
        Subject currentUser = SecurityUtils.getSubject();
        //判断当前用户有没有登录
        if (currentUser.isAuthenticated()) {
            //获取当前用户
            Session session = currentUser.getSession();
           CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
            WxShop shop = wxShopService.getShopInforByUserIdAndShopStatus(currentUserModel.getUserId(),2);
            /**如果用户没有店铺就跳转首页*/
            if (shop==null){
                return "redirect:/home";
            }
            if (shop!=null){
                model.addAttribute("shop",shop);
            }
            model.addAttribute("WxShopModel", new WxShopModel());
        } else {
            //判断有没有登录没有登录就跳转登录页面
            return "redirect:/login";
        }
        return "shop_name";
    }
    /***
     * 修改店铺名Post
     */
    @RequestMapping(value = "modify/shop_name", method = RequestMethod.POST)
    public String modifyShopShopNamePost(Model model,@RequestParam("shopName") String shopName ) {
        Subject currentUser = SecurityUtils.getSubject();
        //判断当前用户有没有登录
        if (currentUser.isAuthenticated()){
        //获取当前用户
        Session session = currentUser.getSession();
       CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
        WxShop shop = wxShopService.getShopInforByUserIdAndShopStatus(currentUserModel.getUserId(),2);
            //这里判断用户自己有没有店铺
        if (shop!=null){
        WxShop wxShop = new WxShop();
        wxShop.setShopId(shop.getShopId());
        wxShop.setShopName(shopName);//店铺名
        int returnResult = wxShopService.update(wxShop);
        if (returnResult>0){
          return "redirect:/wx/shop_setup";
        }
        }
        }else{
            return "redirect:/login";
        }
        return "shop_name";
    }
    /***
     * 修改店铺主营get
     */
    @RequestMapping(value = "modify/shop_main_product", method = RequestMethod.GET)
    public String modifyShopMainProductGet(Model model) {
        Subject currentUser = SecurityUtils.getSubject();
        //判断当前用户有没有登录
        if (currentUser.isAuthenticated()) {
            //获取当前用户
            Session session = currentUser.getSession();
           CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
            WxShop shop = wxShopService.getShopInforByUserIdAndShopStatus(currentUserModel.getUserId(),2);
            /**如果用户没有店铺就跳转首页*/
            if (shop==null){
                return "redirect:/home";
            }
            if (shop!=null){
                model.addAttribute("shop",shop);
            }
            model.addAttribute("WxShopModel", new WxShopModel());
        } else {
            //判断有没有登录没有登录就跳转登录页面
            return "redirect:/login";
        }
        return "shop_main_product";
    }
    /***
     * 修改店铺名Post
     */
    @RequestMapping(value = "modify/shop_main_product", method = RequestMethod.POST)
    public String modifyShopShopMainProductPost(Model model,@RequestParam("shopMainProduct") String shopMainProduct) {
        Subject currentUser = SecurityUtils.getSubject();
        //获取当前用户
        Session session = currentUser.getSession();
       CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
        WxShop shop = wxShopService.getShopInforByUserIdAndShopStatus(currentUserModel.getUserId(),2);
        //这里判断用户自己有没有店铺
        if (shop!=null){
            WxShop wxShop = new WxShop();
            wxShop.setShopId(shop.getShopId());
            wxShop.setShopMainProduct(shopMainProduct);
            int returnResult = wxShopService.update(wxShop);
            if (returnResult>0){
                return "redirect:/wx/shop_setup";
            }
        }
        return "shop_main_product";
    }
    /***
     * 修改店铺介绍Get
     */
    @RequestMapping(value = "modify/shop_summary", method = RequestMethod.GET)
    public String modifyShopSummaryGet(Model model) {
        Subject currentUser = SecurityUtils.getSubject();
        //判断当前用户有没有登录
        if (currentUser.isAuthenticated()) {
            //获取当前用户
            Session session = currentUser.getSession();
            CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
            WxShop shop = wxShopService.getShopInforByUserIdAndShopStatus(currentUserModel.getUserId(),2);
            /**如果用户没有店铺就跳转首页*/
            if (shop==null){
                return "redirect:/home";
            }
            if (shop!=null){
                model.addAttribute("shop",shop);
            }
            model.addAttribute("WxShopModel", new WxShopModel());
        } else {
            //判断有没有登录没有登录就跳转登录页面
            return "redirect:/login";
        }
        return "shop_summary";
    }
    /***
     * 修改店铺介绍post
     */
    @RequestMapping(value = "modify/shop_summary", method = RequestMethod.POST)
    public String modifyShopSummaryPost(Model model,@RequestParam("shopSummary") String shopSummary) {
        Subject currentUser = SecurityUtils.getSubject();
        //获取当前用户
        Session session = currentUser.getSession();
       CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
        WxShop shop = wxShopService.getShopInforByUserIdAndShopStatus(currentUserModel.getUserId(),2);
        //这里判断用户自己有没有店铺
        if (shop!=null){
//            更新店铺介绍
            WxShop wxShop = new WxShop();
            wxShop.setShopId(shop.getShopId());
            wxShop.setShopSummary(shopSummary);
            int returnResult = wxShopService.update(wxShop);
            if (returnResult>0){
                return "redirect:/wx/shop_setup";
            }
        }
        return "shop_summary";
    }

}
